﻿using Dapper_Core.BLL.Impl;
using Dapper_Core.BLL.Interface;
using Dapper_Core.Common;
using Dapper_Core.DAL.Impl;
using Dapper_Core.Model;
using Dapper_Core.Table;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Dapper_Core.Web
{
    public static class SelectExt
    {
        #region 资源类型
        public static IHtmlContent DDLMenuType(this IHtmlHelper htmlHelper, string name, object htmlAttributes)
        {
            return DDLMenuType(htmlHelper, name, null, htmlAttributes);
        }

        public static IHtmlContent DDLMenuType(this IHtmlHelper htmlHelper, string name, string selectedValue, object htmlAttributes)
        {
            try
            {
                Expression<Func<Sys_Dictionary, bool>> exp = dictionary => dictionary.IsActive == 1 && dictionary.IsDeleted == 0 && dictionary.ParentCode == "ResourceType";
                List<Sys_Dictionary> dictionaries = new Sys_DictionaryBusiness().GetList(exp).ToList();
                SelectList list = new SelectList(dictionaries, "DictCode", "DictName", selectedValue);
                return htmlHelper.DropDownList(name, list, htmlAttributes);
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "SelectExt.DDLMenuType()执行失败", "name=" + name + "，selectedValue=" + selectedValue + "，htmlAttributes=" + htmlAttributes, ex, "DapperCore");
                SelectList list = new SelectList(new List<Sys_Dictionary>(), "DictCode", "DictName", selectedValue);
                return htmlHelper.DropDownList(name, list, htmlAttributes);
            }
        }
        #endregion

        #region 厂商类型
        public static IHtmlContent DDLManufacturerType(this IHtmlHelper htmlHelper, string name, object htmlAttributes)
        {
            return DDLManufacturerType(htmlHelper, name, null, htmlAttributes);
        }

        public static IHtmlContent DDLManufacturerType(this IHtmlHelper htmlHelper, string name, string selectedValue, object htmlAttributes)
        {
            try
            {
                Expression<Func<Sys_Dictionary, bool>> exp = dictionary => dictionary.IsActive == 1 && dictionary.IsDeleted == 0 && dictionary.ParentCode == "ManufacturerType";
                List<Sys_Dictionary> dictionaries = new Sys_DictionaryBusiness().GetList(exp).ToList();
                SelectList list = new SelectList(dictionaries, "DictCode", "DictName", selectedValue);
                return htmlHelper.DropDownList(name, list, htmlAttributes);
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "SelectExt.DDLManufacturerType()执行失败", "name=" + name + "，selectedValue=" + selectedValue + "，htmlAttributes=" + htmlAttributes, ex, "DapperCore");
                SelectList list = new SelectList(new List<Sys_Dictionary>(), "DictCode", "DictName", selectedValue);
                return htmlHelper.DropDownList(name, list, htmlAttributes);
            }
        }
        #endregion

        #region 一级部门，数据源是Sys_Department
        /// <summary>
        /// 获取一级部门，数据源是Sys_Department
        /// </summary>
        /// <param name="htmlHelper">扩展方法所属类</param>
        /// <param name="name">name属性的值</param>
        /// <param name="htmlAttributes">自定义html属性键值对</param>
        /// <param name="isYanfa">是否为研发部门（0不是、1是、null不判断是否为研发部门）</param>
        /// <param name="isHistory">是否为历史数据（0不是、1是、null不判断是否为历史数据）</param>
        /// <returns></returns>
        public static IHtmlContent DDLDepartment1(this IHtmlHelper htmlHelper, string name, object htmlAttributes, int? isYanfa = null, int? isHistory = null, bool addTopItem = false)
        {
            return DDLDepartment1(htmlHelper, name, null, htmlAttributes, isYanfa, isHistory);
        }

        /// <summary>
        /// 获取一级部门，数据源是Sys_Department
        /// </summary>
        /// <param name="htmlHelper">扩展方法所属类</param>
        /// <param name="name">name属性的值</param>
        /// <param name="selectedValue">选中项</param>
        /// <param name="htmlAttributes">自定义html属性键值对</param>
        /// <param name="isYanfa">是否为研发部门（0不是、1是、null不判断是否为研发部门）</param>
        /// <param name="isHistory">是否为历史数据（0不是、1是、null不判断是否为历史数据）</param>
        /// <param name="addTopItem">是否在顶部插入请选择项，值为""</param>
        /// <returns></returns>
        public static IHtmlContent DDLDepartment1(this IHtmlHelper htmlHelper, string name, string selectedValue, object htmlAttributes, int? isYanfa = null, int? isHistory = null, bool addTopItem = false)
        {
            Sys_DepartmentBusiness business = new Sys_DepartmentBusiness();
            List<Sys_Department> dept1List = business.GetDept1List(isYanfa, isHistory, addTopItem);
            SelectList list = new SelectList(dept1List, "Dept1Code", "Dept1Name", selectedValue);
            return htmlHelper.DropDownList(name, list, htmlAttributes);
        }
        #endregion

        #region 获取所有的申请类别
        public static IHtmlContent DDLApplyType(this IHtmlHelper htmlHelper, string name, object htmlAttributes, int isActive = 1, bool hasExtra = false)
        {
            return DDLApplyType(htmlHelper, name, null, htmlAttributes, isActive, hasExtra);
        }

        public static IHtmlContent DDLApplyType(this IHtmlHelper htmlHelper, string name, string selectedValue, object htmlAttributes, int isActive = 1, bool hasExtra = false)
        {
            Expression<Func<Sys_Dictionary, bool>> exp = dict => dict.IsActive == isActive && dict.ParentCode == "101";
            List<Sys_Dictionary> dictionaries = new Sys_DictionaryBusiness().GetList(exp, hasExtra);
            SelectList list = new SelectList(dictionaries, "DictCode", "DictName", selectedValue);
            return htmlHelper.DropDownList(name, list, htmlAttributes);
        }
        #endregion

        #region 审批状态
        public static IHtmlContent DDLCheckStatus(this IHtmlHelper htmlHelper, string name, object htmlAttributes, int isActive = 1, bool hasExtra = false)
        {
            return DDLCheckStatus(htmlHelper, name, null, htmlAttributes, isActive, hasExtra);
        }

        public static IHtmlContent DDLCheckStatus(this IHtmlHelper htmlHelper, string name, string selectedValue, object htmlAttributes, int isActive = 1, bool hasExtra = false)
        {
            Expression<Func<Sys_Dictionary, bool>> exp = dict => dict.IsActive == isActive && dict.ParentCode == "104";
            List<Sys_Dictionary> dictionaries = new Sys_DictionaryBusiness().GetList(exp, hasExtra);
            SelectList list = new SelectList(dictionaries, "DictCode", "DictName", selectedValue);

            return htmlHelper.DropDownList(name, list, htmlAttributes);
        }
        #endregion
    }
}
